package com.kuaiditu.assistant.ui.balance;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.kuaiditu.assistant.R;
import com.kuaiditu.assistant.api.assistant_api.AssistantApi;
import com.kuaiditu.assistant.api.assistant_api.base.AssistantResponse;
import com.kuaiditu.assistant.base.BaseActivity;
import com.kuaiditu.assistant.entity.AssistantUser;
import com.kuaiditu.assistant.module.retrofit.ProgressSubscriber;
import com.kuaiditu.assistant.ui.dialog.TaskInfoDialog;
import com.kuaiditu.assistant.util.ToastUtil;
import com.kuaiditu.assistant.view.topbar.TopBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = FillAliCodeActivity.PATH)
public class FillAliCodeActivity extends BaseActivity {


    public static final String PATH = "/user/fill_alicode";

    @BindView(R.id.top_bar)
    TopBar topBar;
    @BindView(R.id.tv_ali_code)
    TextView tvAliCode;
    @BindView(R.id.ll_ali_account)
    LinearLayout llAliAccount;
    @BindView(R.id.tv_ali_name)
    TextView tvAliName;
    @BindView(R.id.ll_ali_name)
    LinearLayout llAliName;


    AssistantApi mAssistantApi;

    AssistantUser mAssistantUser;
    String account;
    String name;

    @Override
    public int initContentView() {
        return R.layout.activity_fill_ali_code;
    }

    @Override
    public void initInjector() {
        mAssistantApi = getApplicationComponent().getApiManager().getAssistantApi();
        mAssistantUser = getApplicationComponent().getUserStorage().getUser();
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        topBar.bind(this, true);

        tvAliCode.setText(mAssistantUser.getCourier_aliCode());
        tvAliName.setText(mAssistantUser.getCourier_aliCodeName());

    }


    @OnClick(R.id.ll_ali_account)
    public void onLlAliAccountClicked(View v) {
        showDialog(tvAliCode);
    }

    @OnClick(R.id.ll_ali_name)
    public void onLlAliNameClicked(View v) {
        showDialog(tvAliName);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem = menu.add("保存");
        menuItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getTitle().equals("保存")) {

            account = tvAliCode.getText().toString();
            name = tvAliName.getText().toString();

            if (TextUtils.isEmpty(account)) {

                ToastUtil.showToast("支付宝账号不能为空");

                return true;
            }
            if (TextUtils.isEmpty(name)) {

                ToastUtil.showToast("真实姓名不能为空");

                return true;
            }

            save(account, name);
        }

        return super.onOptionsItemSelected(item);
    }


    private void save(final String account, final String name) {
        mAssistantApi.saveAliCode(account, name)
                .subscribe(new ProgressSubscriber<AssistantResponse>(mContext) {
                    @Override
                    public void onNext(AssistantResponse assistantResponse) {
                        mAssistantUser.setCourier_aliCode(account);
                        mAssistantUser.setCourier_aliCodeName(name);
                        getApplicationComponent().getUserStorage().saveUser(mAssistantUser);
                        finish();
                    }

                    @Override
                    public void onFinish() {

                    }
                });
    }


    private void showDialog(final TextView tv) {

        final EditText info = (EditText) LayoutInflater.from(mContext).inflate(R.layout.dialog_default_edit_layout, null);
        TaskInfoDialog taskInfoDialog = (TaskInfoDialog) ARouter.getInstance().build(TaskInfoDialog.PATH).navigation();
        taskInfoDialog.setCustomView(info);
        info.setText(tv.getText());

        taskInfoDialog.setOnTakeTaskClickListener("确定", new TaskInfoDialog.onTakeTaskClickListener() {
            @Override
            public void onClick() {

                tv.setText(info.getText().toString());

            }
        });
        taskInfoDialog.show(getSupportFragmentManager(), TaskInfoDialog.PATH);
    }

}
